﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using HeartLess.Managers;

namespace HeartLess.Activities
{
    public class MainMenuActivity : Activity
    {

        private SpriteFont _menuFont;
        private int _selected = 1;

        // These strings are centered and stacked in the main menu
        private static String[] _menu = new String[]
        {
            "Heartless",
            "Start Game",
            "Quit Game"
        };
        private static Color _normalColor = Color.White;
        private static Color _titleColor = Color.Yellow;
        private static Color _selectColor = Color.Red;
        private static Color _bgColor = Color.DarkGray;
        
        public MainMenuActivity()
        {
            _menuFont = AssetManager.GetSpriteFont(AssetManager.FONTS.MAIN_MENU);
            InputManager.Input.DirectionalRepeatAllowed = false;
        }

        public override void Update(GameTime gameTime)
        {
            // In update use the input to get the entry in the menu
            if (InputManager.Input.IsPressingPause)
            {
                switch (_selected)
                {
                    case 1:
                        InputManager.Input.DirectionalRepeatAllowed = true;
                        ActivityManager.CurrentActivity = new GameActivity();
                        break;
                    case 2:
                        HeartlessGame.Game.Exit();
                        break;
                }
            }
            else if (InputManager.Input.IsMovingDown)
                _selected++;
            else if (InputManager.Input.IsMovingUp)
                _selected--;
            if (_selected < 1)
                _selected = _menu.Count() - 1;
            else if (_selected >= _menu.Count())
                _selected = 1;
            //throw new NotImplementedException();
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            // Clear the menu to the bg color
            spriteBatch.GraphicsDevice.Clear(_bgColor);

            // Draw each line equally spaced in the title safe area
            Viewport vp = spriteBatch.GraphicsDevice.Viewport;
            int height = vp.TitleSafeArea.Height;
            int offY = vp.TitleSafeArea.Y;                  // Offset to the title safe area
            int itemSp = height / (_menu.Count() + 1);      // Space for each menu line

            Vector2 stringWH = _menuFont.MeasureString(_menu[0]);
            spriteBatch.DrawString(_menuFont, _menu[0],
                new Vector2((vp.Width - stringWH.X) / 2, (itemSp - stringWH.Y) / 2 + offY),
                _titleColor);

            for (int i = 1; i < _menu.Count(); ++i)
            {
                stringWH = _menuFont.MeasureString(_menu[i]);
                spriteBatch.DrawString(_menuFont, _menu[i],
                    new Vector2((vp.Width - stringWH.X) / 2, (i + 1) * itemSp + (itemSp - stringWH.Y) / 2 + offY),
                    i == _selected ? _selectColor : _normalColor);
            }
        }
    }
}
